package cn.gson.crmdemo.controller.finance;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.dao.*;

import cn.gson.crmdemo.model.pojo.*;
import cn.gson.crmdemo.model.service.HkService;
import cn.gson.crmdemo.model.vo.HkjhVo;

@Controller
public class HkController {
	@Autowired
	HkService hs;
	@Autowired
	HkOrder odao;
	@Autowired
	HkDao hkdao;
	//查询回款计划
	public Map<String,Object> yy(JSONArray selecthk,Page page) {
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("list",selecthk);
		map.put("total",page.getTotal());
		return map;
	}
	@RequestMapping("hk" )
	@ResponseBody
	public Map<String,Object>hk(Integer pageNum,Integer pasgeSize,String planDate,String customerName,Integer isAvailability){
				//分页
				Page<Object> page = PageHelper.startPage(pageNum,pasgeSize);
				//查询数据
				JSONArray selecthk=hs.selecthk("%"+planDate+"%","%"+customerName+"%", isAvailability);
				//创建Map集合
				return this.yy(selecthk, page);

	}
	//添加回款计划
	@RequestMapping("tjhkjh")
	public String addhkjl(HkjhVo tjhkjh){
			Getmoneyplan gt=new Getmoneyplan();
			Order od=new Order();
			od.setOId(tjhkjh.getoId());
			gt.setOrder(od);
			gt.setPlanDate(tjhkjh.getPlanDate());
			gt.setPlanForeignRemark(tjhkjh.getPlanForeignRemark());
			gt.setPlanMoney(new BigDecimal(tjhkjh.getPlanMoney()));
			gt.setUserName(tjhkjh.getUserName());
			gt.setIsAvailability(0);
			hs.tjhkjl(gt);
			return "redirect:hk?pageNum=1&pasgeSize=7";
	}
	
	//删除回款计划
	@RequestMapping("schkjh")
	public String schkjh(Integer getplanId) {
		hs.schkjh(getplanId);
		System.err.println(getplanId);
		return "redirect:hk?pageNum=1&pasgeSize=7";
	}
	//编辑回款计划
	@RequestMapping("bjhkjh")
	public String bjhkjh(HkjhVo hkjh) {
		Getmoneyplan gt=hkdao.findById(hkjh.getGetplanId()).get();
		Order od=odao.findById(hkjh.getoId()).get();
		od.setOId(hkjh.getoId());
		gt.setOrder(od);
		gt.setGetplanId(hkjh.getGetplanId());
		gt.setPlanDate(hkjh.getPlanDate());
		gt.setPlanForeignRemark(hkjh.getPlanForeignRemark());
		gt.setPlanMoney(new BigDecimal(hkjh.getPlanMoney()));
		gt.setIsAvailability(hkjh.getIsAvailability());
		gt.setUserName(hkjh.getUserName());
		hs.tjhkjl(gt);
		return "redirect:hk?pageNum=1&pasgeSize=7";
	}
	//模糊查询
	@RequestMapping("mhcxhh")
	@ResponseBody
	public Map<String,Object>hk(Integer pageNum,Integer pasgeSize,Integer i,String rq){
		//分页
		Page<Object> page = PageHelper.startPage(pageNum,pasgeSize);
		//查询数据
		JSONArray selecthk=hs.selectmh("%"+rq+"%");
		//创建Map集合
		return this.yy(selecthk, page);
}
	//已回查询
	@RequestMapping("yhcx")
	@ResponseBody
	public Map<String,Object>yh(Integer pageNum,Integer pasgeSize,Integer i){
		//分页
		Page<Object> page = PageHelper.startPage(pageNum,pasgeSize);
		//查询数据
		JSONArray selecthk=hs.selectyh(i);
		//创建Map集合
		return this.yy(selecthk, page);
}
	
	//查询订单
	@RequestMapping("dd")
	@ResponseBody
	public Map<String,Object>dd(Integer pageNum,Integer pSize){
		
		//分页
		Page<Object> page = PageHelper.startPage(pageNum,pSize);
		//查询数据
		JSONArray selecthk=hs.selectdd();
		//创建Map集合
		return this.yy(selecthk, page);
}
	
	
	//根据客户查询
	@RequestMapping("hk-khcx")
	@ResponseBody
	public Map<String,Object>selectkh(Integer pageNum,Integer pasgeSize,String kh){
		//分页
		Page<Object> page = PageHelper.startPage(pageNum,pasgeSize);
		//查询数据
		JSONArray selecthk=hs.selectkh("%"+kh+"%");
		//创建Map集合
		return this.yy(selecthk, page);
}
	
	
}
